<script type='text/javascript' src='https://www.google.com/jsapi'></script>
<?php
/* @var $this MemberController */
/* @var $model Member */
$this->layout = "//layouts/column1";
if(Yii::app()->user->isAdmin()){
    $user = Member::model()->findByAttributes(array('username' => 'admin'));
}else{
    $user = Member::model()->findByAttributes(array('member_id' => Yii::app()->user->id));
}

//traverseTree($adminUser, null);


function traverseTree($rootNode, $parentNode) {
    if ($rootNode->a_child_id != null){
        traverseTree(Member::model()->findByAttributes(array('member_id' => $rootNode->a_child_id)), $rootNode);
    }
    if ($rootNode->b_child_id != null){
        traverseTree(Member::model()->findByAttributes(array('member_id' => $rootNode->b_child_id)), $rootNode);
    }
    //echo $rootNode->username.$parentNode->username.'\n\n';
    if($rootNode->parent_id != null){
        $refUser = Member::model()->findByAttributes(array('member_id' => $rootNode->parent_id));
    }
    $package = Package::model()->findByAttributes(array('package_id' => $rootNode->package_id));
    
    $node = '[{v:\''.$rootNode->username.'\',';
    $node = $node.'f:\''.$rootNode->username.'<div style="color:red; font-size: 11px">'.$rootNode->member_type.'</div>';
    $node = $node.'<div style="color:green; font-style:italic">Ref: '.$refUser->username.'</div>';
    $node = $node.'<div style="">A CP Point: '.$rootNode->a_point.'</div>';
    $node = $node.'<div style="">B CP Point: '.$rootNode->b_point.'</div>';
    $node = $node.'<div style="">'.$package->name.'('.$package->point.')'.'</div>';
    $node = $node.'\'},';
    $node = $node.' \''.$parentNode->username.'\',';
    $node = $node.' \'\'],';
    echo $node;
}
?>

<h1>Member Graph</h1>
<div id='graph_div'></div>
<script type='text/javascript'>
    google.load('visualization', '1', {packages:['orgchart']});
    google.setOnLoadCallback(drawChart);
    function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Name');
        data.addColumn('string', 'Manager');
        data.addColumn('string', 'ToolTip');
        data.addRows([
              <?php 
              traverseTree($user, null);                
              ?>  
        ]);
        var chart = new google.visualization.OrgChart(document.getElementById('graph_div'));
        chart.draw(data, {allowHtml:true});
    }
</script>

